package recordsDatabase;



import java.util.ArrayList;


public class ListOfCategories  {
	

	// create the list
	private ArrayList<Category> categoriesList ;
	
	private static volatile ListOfCategories instance;

	// This method is called getInstance. It uses a double - locking mechanism
	// to ensure
	// synchronization between threads when accessed so that no more than one
	// instance is created.
	// Finally the instance of the single List of records is returned.

	public static ListOfCategories getInstance() {
		if (instance == null) {
			synchronized (ListOfCategories.class) {
				if (instance == null)
					instance = new ListOfCategories();
			}
		}
		return instance;
	}
	
	public ListOfCategories(){
		categoriesList = new ArrayList<Category>(); 
	}
	
	public ArrayList<Category> getCategoriesList(){
		return categoriesList;
	}
	
	public void addEntry(Category c){
		categoriesList.add(c);
		categoriesList.trimToSize();
	}
	
	public void reset(){
		categoriesList.clear();
		ListOfCategoryDetails.getInstance().reset();
		
	}
	
	public Category getCategory(String category){
		Category tmpCategory = null;
			for (Category c: categoriesList){
				if (c.getCategoryName().equals(category)){
					return c;
				}
			} 
		return tmpCategory;
	}
	
	public boolean categoryExists(String category){
		for (Category c:categoriesList){
			if (c.getCategoryName().equalsIgnoreCase(category)){return true;}
		}
	return false;
	}
	
}
